﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Silvermoon.Visuals;
using Silvermoon.OpenGL;
using Silvermoon.OpenGL.Native;
using Silvermoon.Core;

namespace Silvermoon.Visuals
{
    /// <summary>
    ///  Abstract <see cref="Visual"/> class.
    ///  A visual represents a rectangle vertex with various textures and colors assigned.
    /// </summary>
    public abstract class Visual : IDisposable
    {
        #region ctor/dtor

        public Visual()
            : base()
        {
            IsInitialized = false;
            Visible = true;
        }

        public virtual void Dispose()
        {
        }

        #endregion
        #region Fields

        private Point location;
        private Size size;
        private int alpha = 255;

        #endregion
        #region Properties

        public bool Visible { get; set; }


        public Point Location
        {
            get { return location; }
            set { location = value; }
        }

        public Rectangle Bounds
        {
            get { return new Rectangle(location.X, location.Y, size.Width, size.Height); }
            set
            {
                location.X = value.X;
                location.Y = value.Y;
                Size = new Size(value.Width, value.Height);
                Invalidate();
            }

        }

        public int X { get { return location.X; } }
        public int Y { get { return location.Y; } }
        public int Width { get { return size.Width; } }
        public int Height { get { return size.Height; } }
        public int Alpha { get { return alpha; } set { if (alpha != value) { alpha = value; Invalidate(); } } }

        public virtual Size Size { get { return size; } set { if (size != value) { size = value; Invalidate(); } } }

        public bool IsInitialized { get; protected set; }

        public IEnumerable<Transformation> Transformations { get; set; }

        #endregion
        #region Methods

        protected virtual void CreateElements()
        {
            IsInitialized = true;
        }

        public virtual void PreLoad()
        {
            if (!IsInitialized) CreateElements();
        }

        public virtual void Invalidate()
        {
            this.IsInitialized = false;
        }

        public static Size MinSize(Size size)
        {
            int w = size.Width;
            int h = size.Height;
            if (w < 1) w = 1;
            if (h < 1) h = 1;
            size.Width = Texture.ConvertToValidTextureDimension(w);
            size.Height = Texture.ConvertToValidTextureDimension(h);

            return size;
        }

        #endregion
        #region primitive
        
        protected virtual Color GetPrimitiveColor()
        {
            return Color.Empty;
        }

        /// <summary>
        /// Gets the color to use for drawing a primitive.
        /// </summary>
        /// <value></value>
        internal Color PrimitiveColor
        {
            get { return GetPrimitiveColor(); }
        }

        /// <summary>
        /// Renders the visual and returns true, otherwise false.
        /// </summary>
        /// <param name="renderer">The renderer to render against and retreive additional data.</param>
        public virtual void Render(Renderer renderer)
        {
        }
        
        #endregion

    }

}
